package org.example.struct.array;

/**
 * @author liyishan
 * @date 2024/5/9 13:08
 * @apiNote
 */

public class Test00476FindComplement {
    public int findComplement(int num) {
        // 找到最高位的1，左移一位并-1，异或
        int highbit = 1;
        int x = num;
        while (x != 0) {
            // 找最低位（最右边）的 1
            highbit = x & (-x);
            // 打掉最低位（最右边）的1
            x = x & (x - 1);
        }
        return num ^ ((highbit << 1) - 1);
    }
}
